/**
 * 
 */
package edu.umd.clip.lm.model;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import edu.umd.clip.lm.factors.FactorTuple;
import edu.umd.clip.lm.util.BinaryPrefix;
import edu.umd.clip.lm.util.XMLizable;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class HFTPayload extends XMLizable {
	public static final String XML_ELEMENT_NAME = "hft-node";
	public static final String XML_TUPLE_NAME = "tuple";
	public BinaryPrefix prefix;
	public long tuple;
	
	public HFTPayload(BinaryPrefix p) {
		this(p, 0);
	}
	
	public HFTPayload(BinaryPrefix p, long t) {
		prefix = p;
		tuple = t;
	}

	public HFTPayload(Element xmlnode) {
		assert(xmlnode.getTagName().equals(XML_ELEMENT_NAME));
		Element prefixElement = (Element) xmlnode.getElementsByTagName(BinaryPrefix.XML_ELEMENT_NAME).item(0);
		this.prefix = new BinaryPrefix(prefixElement);
		NodeList nodelist = xmlnode.getElementsByTagName(XML_TUPLE_NAME);
		if (nodelist != null && nodelist.getLength() == 1) {
			String tupleString = nodelist.item(0).getTextContent();
			this.tuple = Experiment.getInstance().getTupleParser().wordToTuple(tupleString);
		}
	}
	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.util.XMLizable#createXML(org.w3c.dom.Document)
	 */
	@Override
	public Element createXML(Document doc) {
		Element element = doc.createElement(XML_ELEMENT_NAME);
		element.appendChild(prefix.createXML(doc));
		if (tuple != 0) {
			Element tupleElement = doc.createElement(XML_TUPLE_NAME);
			tupleElement.setTextContent(FactorTuple.toStringNoNull(tuple));
			element.appendChild(tupleElement);
		}
		return element;
	}

	/* (non-Javadoc)
	 * @see edu.umd.clip.lm.util.XMLizable#getElementName()
	 */
	@Override
	public String getElementName() {
		return XML_ELEMENT_NAME;
	}
}
